<template>
  <div class="cpas">
    <el-form
      class="input-form"
      label-position="right"
      label-width="120px"
      ref="editForm"
      :rules="propRules"
      :model="form"
    >
      <div class="form-unit">待加工耗材信息</div>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="品名">
            <el-input v-model="form.matrName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="品规">
            <el-input v-model="form.matrSpec" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="效期">
            <el-input v-model="form.expDate" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="批次">
            <el-input v-model="form.lot" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="sn">
            <el-input v-model="form.sn" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="rfid">
            <el-input v-model="form.rfid"
                      :disabled="rfid != null || form.sn == null"
                      placeholder="在此设置光标，开始扫描RFID"
                      @keyup.enter.native="txtScan(form.rfid)" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="仓库">
            <el-input v-model="form.warehouseName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="库位">
            <el-input v-model="form.locationName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item>
            <el-checkbox v-model="form.flagConsignment" disabled>寄售入库</el-checkbox>
            <el-checkbox v-model="form.flagOmoc" disabled>一物一码</el-checkbox>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="加工包装">
            <el-select
              v-model="form.packageUnit"
              filterable
              placeholder="请选择包装"
              @change="matrUnitChange"
              :disabled="form.sn != null"
            >
              <el-option
                v-for="item in matrUnits"
                :key="item.unit"
                :label="item.unit"
                :value="item.unit"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="加工数量">
            <el-input-number v-model="form.packageQty" :min="0" :max="maxQty" label="加工数量" :disabled="form.sn != null"></el-input-number>
            <el-checkbox v-model="autoSN" disabled>生成SN码</el-checkbox>
            <el-button
              type="primary"
              style="margin-left:50px"
              @click="StockProcess"
              :disabled="form.packageQty==0 || form.sn != null"
            >加工</el-button>
          </el-form-item>
        </el-col>
      </el-row>
      <div class="form-unit">加工单列表</div>
      <el-row>
        <div style="height:500px;">
          <data-view
            ref="childDataView"
            propViewKey="wm_stock_barcode"
            propViewForm="wm/stock-barcode/detail.vue"
            :propPreQueryWrapper="cpChildViewQueryWrapper"
            @extra-button-click="handleExtraButton"
          />
        </div>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import barcodeParse from '@/util/barcodeParse.js'

export default {
  data() {
    return {
      form: {},
      viewData: {},
      maxQty: 1,
      autoSN: true, //自动生成SN
      packageUnits: [],
      sn: '',
      rfid: null,
    }
  },
  created() {
    this.$emit('event-register', {
      validate: (callback) => {
        this.$refs.editForm.validate(callback)
      },
      beforeSave: () => {
        this.$emit('data-callback', this.form)
        return true
      },
      afterMove: (formData, initData) => {
        this.form = global.utils.jsonClone(formData)
        this.rfid = this.form.rfid
        this.maxQty = formData.packageQty
        this.fetchPackageUnit(formData.matrId)
      },
      afterSave: () => {},
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {},
      beforeFlow: flowButtonKey => {
        return true
      },
      afterFlow: flowButtonKey => {},
      beforeClose: () => {
        return true
      }
    })
  },
  props: {
    propParent: Object,
    propInitData: Object,
    propPrimaryKey: String,
    propDictionaries: Object,
    propCurrentRow: Object,
    propViewFormData: Object,
    propReadonly: Boolean,
    propStatus: String,
    propRules: Object
  },
  computed: {
    cpChildViewQueryWrapper() {
      let wrapper = this.$newWrapper()
      wrapper.eq('id', this.form.id)
      return wrapper
    },
    //针对未编码的库存 一物一码的可以且只能选择最小单位 非一物一码的能选择最小单位之外的其他单位
    //针对已编码的库存（非一物一码）只能选择非原先单位和最小单位的其他单位
    matrUnits() {
      if (!this.form.flagUniqueCode) {
        if (this.form.flagOmoc) {
          return this.packageUnits.filter(unit => unit.flagBase == 1)
        } else {
          return this.packageUnits
          // return this.packageUnits.filter(unit => unit.flagBase != 1)
        }
      } else if (!this.form.flagOmoc) {
        return this.packageUnits.filter(unit => unit.unit != this.form.packageUnit)
        // return this.packageUnits.filter(unit => unit.flagBase != 1 && unit.unit != this.form.packageUnit)
      }
      return this.packageUnits
    }
  },
  watch: {},
  components: {},
  mounted() {},
  methods: {
    handleExtraButton(para) {
      if (para.button.key == 'print') {
        const params = {
          labelKey: 'sn',
          data: para.rows
        }
        this.$ajax.post('/sys/printer/printLabel', params).then(res => {
          this.utilMessage.success('已推送打印标签')
        })
      }
    },
    fetchPackageUnit(matrId) {
      this.$ajax.get('/base/matrUnit/getByMatr', { params: { matrId } }).then(resp => {
        this.packageUnits = resp.data
      })
    },
    //更换包装单位后自动计算可加工数量form
    matrUnitChange(matrUnit) {
      let changeRate = this.packageUnits.filter(pu => pu.unit == matrUnit).map(pu => pu.rate)[0]
      this.form.packageQty = Math.floor(this.form.skuQty / changeRate)
      this.maxQty = this.form.packageQty
    },
    StockProcess() {
      this.$ajax.post('/wm/stock/pack', this.form).then(resp => {
        this.form = resp.data
        this.$refs.childDataView.fetchData()
      })
    },
    txtScan(code) {
      let codeType = barcodeParse.barCodeType(code)
      if (codeType == 'RFID') {
        const params = { sn: this.form.sn, rfid: code }
        this.$ajax.post('/wm/stock/binding_rfid', params).then(resp => {
          this.$nextTick(() => {
            this.$refs.childDataView.fetchData()
          })
        })
      } else {
        this.utilMessage.error('RFID格式错误')
      }
      this.rfid = this.form.rfid
    },
  },
  //扩展加工按钮
  extraButtonClick({ button, data, index }) {}
}
</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100%;
}
</style>
